package com.niodata.dp.monitor.sharelib.service;

import com.niodata.dp.monitor.sharelib.entity.base.BaseResult;
import com.niodata.dp.monitor.sharelib.entity.file.AlarmType;
import com.niodata.dp.monitor.sharelib.entity.file.DataSetPartition;
import com.niodata.dp.monitor.sharelib.entity.file.ErrorFilesInfo;
import com.niodata.dp.monitor.sharelib.entity.file.PartitionStatistics;
import com.niodata.dp.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component("dataSetPartitionService")
public class DataSetPartitionService {

  @Autowired
  private RestTemplate restTemplate;

  @Value("${monitor.server.address}")
  private String server;
  @Value("${monitor.server.port}")
  private String port;

  //获取子partitions
  public BaseResult<DataSetPartition> getChildrenByParentId(String dataSetId, String parentId,
        int offset, int count) {
    if (StringUtil.isEmpty(parentId)) {
      parentId = "";
    }
    String execUrl = String.format(
          "http://%s:%s/monitor/file/partition/children?dataSetId=%s&parentId=%s&offset=%s&count=%s",
          server, port, dataSetId, parentId, offset, count);

    return restTemplate.exchange(execUrl, HttpMethod.GET, null,
          new ParameterizedTypeReference<BaseResult<DataSetPartition>>() {
          }).getBody();
  }

  public BaseResult<DataSetPartition> getFullPartitionsPaged(String dataSetId,
        boolean showMissingPartition, boolean showIncompletePartition, int missingFileCount,
        int offset, int count) {
    String execUrl = String.format(
          "http://%s:%s/monitor/file/partition/list?dataSetId=%s&showMissingPartition=%s"
                + "&showIncompletePartition=%s&missingFileCount=%s&offset=%s&count=%s", server,
          port,
          dataSetId, showMissingPartition, showIncompletePartition, missingFileCount, offset,
          count);

    return restTemplate.exchange(execUrl, HttpMethod.GET, null,
          new ParameterizedTypeReference<BaseResult<DataSetPartition>>() {
          }).getBody();
  }

  public BaseResult<PartitionStatistics> getPartitionStatistics(String dataSetId) {
    String execUrl = String.format(
          "http://%s:%s/monitor/file/partition/statistics?dataSetId=%s", server, port, dataSetId);

    return restTemplate.exchange(execUrl, HttpMethod.GET, null,
          new ParameterizedTypeReference<BaseResult<PartitionStatistics>>() {
          }).getBody();
  }

  public BaseResult<String> getPartitionFullPath(String dataSetId, String partitionId) {
    if (StringUtil.isEmpty(partitionId)) {
      partitionId = "";
    }
    String execUrl = String.format(
          "http://%s:%s/monitor/file/partition/path?dataSetId=%s&partitionId=%s", server, port,
          dataSetId, partitionId);

    return restTemplate.exchange(execUrl, HttpMethod.GET, null,
          new ParameterizedTypeReference<BaseResult<String>>() {
          }).getBody();
  }


  public BaseResult<ErrorFilesInfo> getErrorFiles(String id, AlarmType alarmType) {
    if (StringUtil.isEmpty(id)) {
      id = "";
    }
    String execUrl = String.format(
          "http://%s:%s/monitor/file/partition/error/files?id=%s&type=%s", server, port, id,
          alarmType.name());

    return restTemplate.exchange(execUrl, HttpMethod.GET, null,
          new ParameterizedTypeReference<BaseResult<ErrorFilesInfo>>() {
          }).getBody();
  }

  public void deleteAllPartitionsByDataSetId(String dataSourceId) {
    if (StringUtil.isEmpty(dataSourceId)) {
      dataSourceId = "";
    }
    String execUrl = String.format(
          "http://%s:%s/monitor/file/partition/del?dataSetId=%s", server, port, dataSourceId);

    restTemplate.postForEntity(execUrl, null, BaseResult.class).getBody();
  }


}
